﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Infrastructure;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class CategoryDao : AbstractDao<Category>, ICategoryDao
    {
        public override bool
        create(Category cat)
        {
            try
            {
                cat.catId = this.createPrefixId();

                _ctx.Categories.InsertOnSubmit(cat);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override bool
        update(Category cat)
        {
            try
            {
                var curr = getById(cat.catId);

                curr.catName = cat.catName;
                curr.catDescription = cat.catDescription;
                curr.catParentId = cat.catParentId;
                curr.belongToDivision = cat.belongToDivision;

                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string
        createPrefixId()
        {
            string currMaxId = getAll().Max(i => i.catId);
            string prefixPart = "cat";
            int nDigits = 4;

            AutoId autoid = new AutoId();

            return autoid.tryCreatePrefixId(currMaxId, prefixPart, nDigits);
        }

        public override Category
        getById(object id)
        {
            return getSingle(i => i.catId.Equals((string)id));
        }

        public List<Category>
        getByName(string catName)
        {
            try
            {
                return getMultiple(i => i.catName.Contains(catName));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public Category
        getByNameExactly(string catName)
        {
            return getSingle(i => i.catName.Equals(catName));
        }

        public List<Category>
        getByCategory(string catId)
        {
            try
            {
                return getMultiple(i => i.catParentId.Equals(catId));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Category>
        getByDivision(string divId)
        {
            try
            {
                return getMultiple(i => i.belongToDivision.Equals(divId));
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region ICategoryDao

    public interface ICategoryDao : IDao<Category>
    {
        String createPrefixId();

        List<Category> getByName(String catName);

        Category getByNameExactly(String catName);

        List<Category> getByCategory(String catId);

        List<Category> getByDivision(String divId);
    }
    #endregion
}